var wg_profile_link = "http://me.zing.vn/";
var wg_root_domain = "http://widget.me.zing.vn";
var wg_is_id = 0;
var wg_ex_en = 0;
var wg_ex_l = 1;
var wg_cb = "";
var wg_uniq = 0;

function fA(holder, value, u, dis) {
	var obj = document.getElementById(holder);
	if (obj != null) {
		var _class = obj.getAttribute("a_class");
		var _width = obj.getAttribute("a_width");
		var _height = obj.getAttribute("a_height");
		var _l =  obj.getAttribute("l");
		if(!_l && _l == null || _l == undefined) _l = 0;
		if(dis == null || dis == undefined || dis == '') {
			dis = "";
		}
		else dis = " title='" + dis + "'";
			
		if (_class != "") _class = "class='" + _class + "'";
		if (_width != "-1") _width = "width='" + _width + "'";
		if (_height != "-1") _class = "height='" + _height + "'";
		var ava = "";
		if(_l == 0)
			ava = "<img " + _class + " " + _width + " src='" + value + "' onerror='this.src=\"" + wg_root_domain + "/images/160.gif\";' " + dis + " />";
		else
			if(_l == 1) ava = "<a href='" + wg_profile_link + u + "'><img border=0 " + _class + " " + _width + " src='" + value + "' onerror='this.src=\"" + wg_root_domain + "/images/160.gif\";' " + dis + " /></a>";
			else if(_l == 2) ava = "<a target='_blank' href='" + wg_profile_link + u + "'><img border=0 " + _class + " " + _width + " src='" + value + "' onerror='this.src=\"" + wg_root_domain + "/images/160.gif\";'" + dis + " /></a>";
			else if(_l == 3) ava = "<a target='_parent' href='" + wg_profile_link + u + "'><img border=0 " + _class + " " + _width + " src='" + value + "' onerror='this.src=\"" + wg_root_domain + "/images/160.gif\";'" + dis + " /></a>";
		obj.innerHTML = ava;
	}
}

function fD(holder, value, v, u) {
	var obj = document.getElementById(holder);	
	if (obj != null) {
		var _cnt = obj.getAttribute("cnt");
		if(v == null || v == undefined)
			obj.innerHTML = _fD(obj,value,u);
		else if(v == 1 && _cnt == undefined)
			obj.innerHTML = "<img src='http://img.me.zdn.vn/images/zme_iconsao_small.png'>" + _fD(obj,value,u);
		else if(v == 2 && _cnt == undefined)
			obj.innerHTML = "<img align='absmiddle' src='http://img.me.zdn.vn/images/zme_iconbiz_small.png'>" + _fD(obj,value,u);
		else
			obj.innerHTML = _fD(obj,value,u);
	}
}

function _fD(obj,value,u) {
	var _l =  obj.getAttribute("l");
	var _ex = obj.getAttribute("ex");
	var _t = obj.getAttribute("t");
	var _cnt = obj.getAttribute("cnt");
	var _c = obj.getAttribute("c");


	if(!_ex || _ex == null || _ex == undefined) _ex = "";
	if(!_t || _t == null || _t == undefined) _t = "";
	if(!_l && _l == null || _l == undefined) _l = 0;


	if(value == "") {
		if(_t == "ex") return "@" + u;
		else return u;
	}

	if(_l == 0) {
		return ZME_cs(value + _ex,_c);

	}
	else {
		if(u == null || u == undefined || u == '')
			return ZME_cs(value + _ex,_c);
		else
		{
			if(_cnt != null&& _cnt != undefined) {
				var _cnt = document.getElementById(_cnt);
				if(_cnt != null) value = _cnt.innerHTML;
			}

			if(_l == 1) return "<a title='" + value + "' href='" + wg_profile_link + u + "'>" + ZME_cs(value,_c) + "</a>" + _ex;
			else if(_l == 2) return "<a target='_blank' title='" + value + "' href='" + wg_profile_link + u + "'>" + ZME_cs(value,_c) + "</a>" + _ex;
			else if(_l == 3) return "<a target='_parent' title='" + value + "' href='" + wg_profile_link + u + "'>" + ZME_cs(value,_c) + "</a>" + _ex;
		}
	}
}

function fE(holder, value, v, u) {
	fD(holder, value, v, u);
}

function ZME_cs(input,letter) {
	if(letter != null && parseInt(letter) > 0) {
		if(input.length < parseInt(letter) + 3) return input;
		return input.substring(0,letter) + "...";
	}
	else return input;
}

function ZME_pass(a,d,ai,di,de)
{
	
	if(a == "" && d == "" && ai == "" && di == "" && de == "") return;
	var size = "160";
	if (window.zme_avatar_size != null && window.zme_avatar_size != undefined)	{
		size = window.zme_avatar_size;
	}
	if(wg_cb != "") {
		var url = wg_root_domain + "/api/widget?method=getMixExCb&size=" + size + "&ava_list=" + a + "&display_list=" + d + "&ava_list_id=" + ai + "&display_list_id=" + di + "&display_list_ex=" + de + "&cb=" + wg_cb;
	}
	else {
		var url = wg_root_domain + "/api/widget?method=getMixEx&size=" + size + "&ava_list=" + a + "&display_list=" + d + "&ava_list_id=" + ai + "&display_list_id=" + di + "&display_list_ex=" + de;
	}
	var script = document.createElement("script");
	script.setAttribute("src",url);
	script.setAttribute("type", "text/javascript");
	script.setAttribute("charset", "utf-8");
	var body_element = document.getElementsByTagName("body");
	

	body_element[0].appendChild(script);
}

function ZME_cb(l) {
	var script = document.createElement("script");
	script.setAttribute("src",l);
	script.setAttribute("type", "text/javascript");
	script.setAttribute("charset", "utf-8");
	var body_element = document.getElementsByTagName("body");
	body_element[0].appendChild(script);
}

function ZME_gP(input,name)
{
	var arr = input.split("?");
	if(arr[1] != null) {
		var arr2 = arr[1].split("&");
		for(i=0;i<arr2.length;i++) {
			var arr3 = arr2[i].split("=");
			if(arr3[0] != null && arr3[0] == name) {
				return arr3[1];
			}
		}
	}
	return null;
}

function ZME_DO(k,i,uniq,input)
{
	var _span = document.createElement('span');
	var _class = "";
	var _pos = input.indexOf("?");
	var _ex = 0;

	if(_pos != -1)
	{
		var temp;
		temp = ZME_gP(input,"class");
		if(temp) _class=temp;

		temp = ZME_gP(input, "ex");
		if(temp && temp == 1) {
			_is_id = 2;
			_span.setAttribute("t", "ex");
		}
		else {
			temp = ZME_gP(input, "id");
			if(temp && temp != 0) _is_id = 1;
			else _is_id = 0;
		}

		temp = ZME_gP(input, "cnt");
		if(temp && temp != undefined) {
			_span.setAttribute("cnt", temp);
		}

		temp = ZME_gP(input, "c");
		if(temp && temp != undefined && temp != "") {
			_span.setAttribute("c", temp);
		}

		temp = ZME_gP(input, "l");
		if(temp && temp != 0) _is_l = temp;
		else _is_l = 0;

		input = input.substr(0,_pos);
		input = ZME_gS(input);
		_pos = input.indexOf("#");
		if (_pos != -1) {
			var arr = input.split("#");
			var extra = arr[1];
			input = arr[0];
			_span.setAttribute("ex", extra);
		}
	}
	else
	{
		_is_id = 0;
		_is_l= 0;
	}

	_span.setAttribute("class",_class);
	_span.setAttribute("l", _is_l);
	_span.id = "ZMED" + uniq;

	k[i].appendChild(_span);
	wg_is_id = _is_id;
	return input;
}

function ZME_AO(k, i, uniq, input,pr) {
	var _span = document.createElement('span');

	var _w = -1;
	var _h = -1;
	var _cl = "";
	var _pos = input.indexOf("?");

	if (window.zme_avatar_width != null && window.zme_avatar_width != undefined) {
		_w = zme_avatar_width;
	}
	if (window.zme_avatar_heigth != null && window.zme_avatar_heigth != undefined) {
		_height = zme_avatar_heigth;
	}

	if (_pos != -1) {
		var temp;
		temp = ZME_gP(input, "width");
		if (temp) _w = temp;
		temp = ZME_gP(input, "height");
		if (temp) _h = temp;
		temp = ZME_gP(input, "class");
		if (temp) _cl = temp;

		temp = ZME_gP(input, "size");
		if(temp) {pr.param = temp;}
		else pr.param = "";

		temp = ZME_gP(input, "id");
		if(temp && temp != 0) _is_id = 1;
		else _is_id = 0;

		temp = ZME_gP(input, "l");
		if(temp && temp != 0) _is_l = temp;
		else _is_l = 0;

		input = input.substr(0, _pos);
		input = ZME_gS(input);
		_pos = input.indexOf("#");
		if (_pos != -1) {
			var arr = input.split("#");
			var extra = arr[1];
			input = arr[0];
			_span.setAttribute("ex", extra);
		}

	}
	else
	{
		_is_id = 0;
		_is_l= 0;
	}

	_span.setAttribute("a_class", _cl);
	_span.setAttribute("a_width", _w);
	_span.setAttribute("a_height", _h);
	_span.setAttribute("l", _is_l);

	_span.id = "ZMEA" + uniq;
	
	if(window.zme_avatar_init != null && window.zme_avatar_init != undefined && window.zme_avatar_init != "") {
		var _width = "";
		var _height = "";
		if (_w != "-1") _width = "width='" + _w + "'";		
		if (_h != "-1") _height = "height='" + _h + "'";
		_span.innerHTML = "<img " + _width + " " + _height + " src='" + window.zme_avatar_init + "'/>";//zme_avatar__init ava = "<img " + _class + " " + _width + " src='" + value + "' onerror='this.src=\"" + wg_root_domain + "/images/160.gif\";' " + dis + " />";
	}


	k[i].appendChild(_span);
	wg_is_id = _is_id;

	return input;
}

function ZME_gS(input) {
	return input.replace(/([a-z0-9]+)([\.|_]+)$/igm,'$1#$2');
}

function ZME_strip(input)
{
	var _pos = input.indexOf("#");
	if(_pos != -1) return input.substr(0,_pos);
	else return input;
}

function ZME_ex() {
	if(wg_ex_en == 1) {
		var _zme = document.body.innerHTML;
		_zme = _zme.replace(/([ |\n|\t|\r|\"|,|>])@([a-z0-9._]+)/igm,'$1<span rel="ZMED_$2?ex=1&l=' + wg_ex_l + '"></span>');
		document.body.innerHTML = _zme;
	}
}
function zme_param() {
	this.param = '';
}

function ZME_widget_mix()
{
	ZME_ex();
	var t = "a";
	if(window.zme_avatar_tag != null && window.zme_avatar_tag != undefined)
	{
		t = window.zme_avatar_tag;
	}

	var k = document.getElementsByTagName(t);
	var a_h = new Array();
	var a_h_id = new Array();
	var d_h = new Array();
	var d_h_id = new Array();
	var d_h_ex = new Array();
	var temp;
	var j = 0;
	var j_d = 0;
	for (var i=0; i<k.length; i++)
	{
		var uniq = wg_uniq++;
		var att =  k[i].getAttribute("rel");
		var att_f = k[i].getAttribute("rel_f");
		if (att && (att_f == null))
		{
			var Me = att.substr(0,5).toUpperCase();
			var Usn =  att.substr(5) ;
			if (Me=="ZMEA_")
			{
				var pr = new zme_param();
				pr.param = "";
				temp = ZME_AO(k, i, uniq, Usn,pr);
				k[i].setAttribute("rel_f", "1");
				if(pr.param != "") j = uniq + "-" + pr.param;
				else j = uniq;
				if(wg_is_id == 1)
				{					
					if (ckHolder(a_h_id, temp))
					{
						a_h_id[temp] += "ZMEA" + j + "|";
					}
					else
					{
						a_h_id[temp] = "ZMEA" + j + "|";
					}
				}
				else
				{
					if (ckHolder(a_h, temp))
					{
						a_h[temp] += "ZMEA" + j + "|";
					}
					else
					{
						a_h[temp] = "ZMEA" + j + "|";
					}
				}
			}
			else if(Me == "ZMED_") {
				temp = ZME_DO(k,i,uniq,Usn);
				k[i].setAttribute("rel_f","1");
				if(wg_is_id == 1)
				{
					if (ckHolder(d_h_id, temp)) {
						d_h_id[temp] += "ZMED" + uniq + "|";
					}
					else {
						d_h_id[temp] = "ZMED" + uniq + "|";
					}
				}
				else if(wg_is_id == 2)
				{
					if (ckHolder(d_h_ex, temp)) {
						d_h_ex[temp] += "ZMED" + uniq + "|";
					}
					else {
						d_h_ex[temp] = "ZMED" + uniq + "|";
					}
				}
				else
				{
					if (ckHolder(d_h, temp)) {
						d_h[temp] += "ZMED" + uniq + "|";
					}
					else {
						d_h[temp] = "ZMED" + uniq + "|";
					}
				}
			}
		}
	}

	var a_l = "";
	var a_i = "";
	var d_l = "";
	var d_i = "";
	var d_e = "";

	var key;
	for(key in a_h)
		a_l += key + "?" + a_h[key] + ",";
	for(key in a_h_id)
		a_i += key + "?" + a_h_id[key] + ",";
	for (key in d_h)
		d_l += key + "?" + d_h[key] + ",";
	for (key in d_h_id)
		d_i += key + "?" + d_h_id[key] + ",";
	for (key in d_h_ex)
		d_e += key + "?" + d_h_ex[key] + ",";

	ZME_pass(a_l,d_l,a_i,d_i,d_e);
	
}

function ckHolder(arr, acc)
{
	for (var a in arr)
	{
		if (a == acc) {
			return true;
		}
	}
	return false;
}

if(window.zme_ex_en != null && window.zme_ex_en != undefined) wg_ex_en= window.zme_ex_en;
if(window.zme_ex_l != null && window.zme_ex_l != undefined) wg_ex_l= window.zme_ex_l;
if(window.zme_wg_cb != null && window.zme_wg_cb != undefined) wg_cb= window.zme_wg_cb;

if(window.zme_avatar_auto != null && window.zme_avatar_auto != undefined && window.zme_avatar_auto == "off"){
}
else{
	window.onload = ZME_widget_mix;
}
